//
//  XMFeedbackDetailViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/26.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMFeedbackDetailViewController.h"
#import "XMFeedbackModel.h"
#import "XMFeedbackDetailImgCell.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface XMFeedbackDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *createTimeLa;
@property (weak, nonatomic) IBOutlet UILabel *stateLa;
@property (weak, nonatomic) IBOutlet UILabel *contentLa;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionH;
@property (weak, nonatomic) IBOutlet UIView *replyView;
@property (weak, nonatomic) IBOutlet UILabel *replyTimeLa;
@property (weak, nonatomic) IBOutlet UILabel *replyContent;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *replayContentH;

@end

@implementation XMFeedbackDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"反馈详情" forState:UIControlStateNormal];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake((kScreenWidth - 60 - 30) / 4, (kScreenWidth - 60 - 30) / 4);

    switch (self.model.images.count) {
        case 0:
            self.collectionH.constant = 0;
            self.collectionView.hidden = YES;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
            self.collectionH.constant = (kScreenWidth - 60 - 30) / 4;
            self.collectionView.hidden = NO;
            break;
        case 5:
        case 6:
        case 7:
        case 8:
            self.collectionH.constant = (kScreenWidth - 60 - 30) / 2 + 10;
            self.collectionView.hidden = NO;
            break;
        case 9:
            self.collectionH.constant = (kScreenWidth - 60 - 30) / 4 * 3 + 20;
            self.collectionView.hidden = NO;
            break;
        default:
            break;
    }
    [self.collectionView setCollectionViewLayout:layout];
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMFeedbackDetailImgCell)) forCellWithReuseIdentifier:kStringFromClass(XMFeedbackDetailImgCell)];
    
    self.createTimeLa.text = self.model.create_time;
    self.contentLa.text = self.model.content;
    self.contentH.constant = [self.model.content heightForFont:kUIFont(12) width:kScreenWidth - 60] + 1;
    
    self.stateLa.text = self.model.status;
    if ([self.model.status isEqualToString:@"处理中"]) {
        self.stateLa.textColor = kAppMainColor;
    } else {
        self.stateLa.textColor = kHEX(@"#44AF35");
    }
    
    self.replyView.hidden = [XMUtil isBlankString:self.model.feedback];
    self.replyTimeLa.text = self.model.handle_time;
    self.replyContent.text = self.model.feedback;
    self.replayContentH.constant = [self.model.feedback heightForFont:kUIFont(12) width:kScreenWidth - 60] + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.images.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMFeedbackDetailImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMFeedbackDetailImgCell) forIndexPath:indexPath];
    XMFileModel *model = [self.model.images safe_objectAtIndex:indexPath.item];
    [cell.imgV sd_setImageWithURL:kStringToUrl(model.file_url) placeholderImage:kDefaultCoverImg];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XMFeedbackDetailImgCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    __block NSMutableArray *datas = [NSMutableArray array];
    [self.model.images enumerateObjectsUsingBlock:^(XMFileModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:obj.file_url];
        data.projectiveView = cell.imgV;
        [datas addObject:data];
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.item;
    [browser show];
}

@end
